package com.llkj.qingnianguoxue.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 热门排行——视频
 */
public class HotRankingAdapter extends BaseQuickAdapter<HotRankingBean.RankingBean, BaseViewHolder> {
    private int mType = 1;//1视频，2图书,3大师详情视频
    private int mTagVisible;//0默认不可见，1可见
    private ClickViewCallback listener;

    public HotRankingAdapter(int layoutResId, List<HotRankingBean.RankingBean> data, ClickViewCallback listener) {
        super(layoutResId, data);
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, HotRankingBean.RankingBean item) {
        ImageView ivPic = baseViewHolder.getView(R.id.pic);
        baseViewHolder.setText(R.id.tv_title, item.getTitle());//
        baseViewHolder.setText(R.id.tv_introduction, item.getAbstractX());
//        if(StringUtils.isEmpty(item.getPrice())){
//            baseViewHolder.setText(R.id.tv_price, "");
//        }else{
//            baseViewHolder.setText(R.id.tv_price, "￥" + item.getPrice()+"银子");
//        }

        FrameLayout.LayoutParams picParam = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        ImageView ivPlay = baseViewHolder.getView(R.id.iv_video_play);
        TextView tvBuy = baseViewHolder.getView(R.id.tv_buy);
        ImageView ivShopCart = baseViewHolder.getView(R.id.iv_add_shop);
        baseViewHolder.addOnClickListener(R.id.iv_add_shop);

        TextView tvTitle = baseViewHolder.getView(R.id.tv_title);
        float priceVideo = 0f;
        try {
            priceVideo = Float.parseFloat(item.getPrice());
        }catch (Exception e){}

        if("1".equals(item.getIs_order())){
            if (1 == item.getType()) {
                tvBuy.setVisibility(View.VISIBLE);
                tvBuy.setText("已订阅");
                tvTitle.setMaxWidth(DisplayUtil.dip2px(mContext,150));
                ivShopCart.setImageResource(R.drawable.icon_hot_shopcart1);
                ivShopCart.setVisibility(View.GONE);
                setListener(baseViewHolder.getLayoutPosition(), ivShopCart, -1);
            }else{
                tvBuy.setVisibility(View.INVISIBLE);
                tvTitle.setMaxWidth(DisplayUtil.dip2px(mContext,150));
                ivShopCart.setImageResource(R.drawable.icon_hot_shopcart1);
                ivShopCart.setVisibility(View.GONE);
                setListener(baseViewHolder.getLayoutPosition(), ivShopCart, -1);
            }

            baseViewHolder.setText(R.id.tv_price, "");
        }else{
            if (1 == item.getType()) {//1未加购物车 2 已加购物车 3 已购
                tvBuy.setVisibility(View.GONE);
                ivShopCart.setVisibility(View.VISIBLE);
                tvTitle.setMaxWidth(DisplayUtil.dip2px(mContext,800));
                ivShopCart.setImageResource(R.drawable.icon_hot_shopcart2);
                setListener(baseViewHolder.getLayoutPosition(), ivShopCart, 1);
            } else if (2 == item.getType()) {
                tvBuy.setVisibility(View.VISIBLE);
                tvBuy.setText("已加入购物车");
                tvTitle.setMaxWidth(DisplayUtil.dip2px(mContext,140));
                ivShopCart.setVisibility(View.VISIBLE);
                ivShopCart.setImageResource(R.drawable.icon_hot_shopcart1);
                setListener(baseViewHolder.getLayoutPosition(), ivShopCart, 1);
            }else if (3 == item.getType()) {
                tvBuy.setVisibility(View.VISIBLE);
                tvBuy.setText("已购买");
                tvTitle.setMaxWidth(DisplayUtil.dip2px(mContext,150));
                ivShopCart.setImageResource(R.drawable.icon_hot_shopcart1);
                ivShopCart.setVisibility(View.VISIBLE);
                setListener(baseViewHolder.getLayoutPosition(), ivShopCart, -1);
            }
            if(priceVideo==0){
                ivShopCart.setVisibility(View.GONE);
                tvBuy.setVisibility(View.GONE);
                baseViewHolder.setText(R.id.tv_price, "免费");
            }else{
                baseViewHolder.setText(R.id.tv_price, "￥" + item.getPrice()+"银子");
            }
        }

        if (mType == 1 || mType == 5) {
            ivPic.setScaleType(ImageView.ScaleType.FIT_XY);
            if(mType==5){
                ivShopCart.setVisibility(View.INVISIBLE);
            }
            ivPlay.setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.tv_video_time, item.getDuration());//时间
            picParam.width = DisplayUtil.getDensityWdith(mContext) * 268 / 750;
            picParam.height = picParam.width * 9 / 16;

        } else if (mType == 2) {
            ivPlay.setVisibility(View.INVISIBLE);
            picParam.width = DisplayUtil.getDensityWdith(mContext) * 150 / 750;
            picParam.height = picParam.width * 14 / 13;
        } else if (mType == 3) {
            baseViewHolder.setText(R.id.tv_video_time, item.getDuration());//时间
            tvBuy.setVisibility(View.INVISIBLE);
//            ivShopCart.setVisibility(View.VISIBLE);
        }else if(mType == 4){//不显示
            baseViewHolder.setText(R.id.tv_video_time, item.getDuration());//时间
            tvBuy.setVisibility(View.INVISIBLE);
            ivShopCart.setVisibility(View.INVISIBLE);
        }
        ivPic.setLayoutParams(picParam);

        if(mTagVisible==1){//设置显示标签
            TextView tagView = baseViewHolder.getView(R.id.tvTag);
            tagView.setVisibility(View.VISIBLE);
            tagView.setText(item.getClassify());//根据分类，显示标签名
        }

        RelativeLayout rlRoot = baseViewHolder.getView(R.id.rl_root);
        setListener(baseViewHolder.getLayoutPosition(), rlRoot, 2);

        ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
    }

    public void setListener(final int position, final View v, final int type) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.clickViewCallback(position, type, v);
                }
            }
        });
    }

    /**
     * 设置视频图书
     *
     * @param type 1视频，2图书，默认视频
     */
    public void setType(int type) {
        mType = type;
    }

    public void setTagVisible(int visible){
        mTagVisible = visible;
    }
}
